package com.example.javabasic.sample.io;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author cph
 */
public class Example {

    private static final int MAX_BUFFER_SIZE = 50 * 1024 * 1024;

    public static void main(String[] args) throws Exception {
//        createFile();
//        absoluteFilePath();
//        workingDirectory();
//        filePermission();

        String str = "";

        downloadVideo(str, "D:/video/", "test", ".mp4");


    }

    private static void downloadVideo(String videoUrl, String savePath, String fileName, String suffixName) throws IOException {

        HttpURLConnection connection = null;
        InputStream inputStream = null;
        RandomAccessFile randomAccessFile = null;
        String filePathName = savePath + fileName + suffixName;
        try {
            URL url = new URL(videoUrl);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("Range", "bytes=0-");
            connection.connect();
            if (connection.getResponseCode() / 100 != 2) {
                System.out.println("连接失败。。。");
                return;
            }
            inputStream = connection.getInputStream();
            randomAccessFile = new RandomAccessFile(filePathName, "rw");
            int downloaded = 0;
            int fileSize = connection.getContentLength();
            while (downloaded < fileSize) {
                byte[] buffer = null;
                if (fileSize - downloaded >= MAX_BUFFER_SIZE) {
                    buffer = new byte[MAX_BUFFER_SIZE];
                } else {
                    buffer = new byte[fileSize - downloaded];
                }
                int read = -1;
                int currentDownload = 0;

                while (currentDownload < buffer.length) {
                    read = inputStream.read();
                    buffer[currentDownload++] = (byte) read;
                }
                randomAccessFile.write(buffer);
                downloaded += currentDownload;
                randomAccessFile.seek(downloaded);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            connection.disconnect();
            inputStream.close();
            randomAccessFile.close();
        }
    }


    private static void filePermission() {
        try {
            File file = new File("D:\\shellscript.sh");

            if (file.exists()) {
                System.out.println("Exectue? " + file.canExecute());
                System.out.println("Write? " + file.canWrite());
                System.out.println("Read? " + file.canRead());
            }

            file.setExecutable(false);
            file.setWritable(false);
            file.setReadable(false);

            System.out.println("Exectue? " + file.canExecute());
            System.out.println("Write? " + file.canWrite());
            System.out.println("Read? " + file.canRead());

            if (file.createNewFile()) {
                System.out.println("file is created.");
            } else {
                System.out.println("file is already existed.");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void workingDirectory() {
        String filename = "newFile2.txt";
        String workingDirectory = System.getProperty("user.dir");

        File file = new File(workingDirectory, filename);

        System.out.println("final path: " + file.getAbsolutePath());

        try {
            if (file.createNewFile()) {
                System.out.println("file is created.");
            } else {
                System.out.println("file is already exists.");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void absoluteFilePath() {
        String filename = "newFile1.txt";
        String workingDirectory = System.getProperty("user.dir");

        String absoluteFilePath = "";
//        absoluteFilePath = workingDirectory + System.getProperty("file.separator") + filename;
        absoluteFilePath = workingDirectory + File.separator + filename;
        System.out.println("final path: " + absoluteFilePath);

        File file = new File(absoluteFilePath);
        try {
            if (file.createNewFile()) {
                System.out.println("File is created.");
            } else {
                System.out.println("File is already existed.");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void createFile() {
        try {
            File file = new File("D:\\newFile.txt");
            if (file.createNewFile()) {
                System.out.println("file created.");
            } else {
                System.out.println("file already exists.");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
